package com.example.studyktdemo.adapter;

import android.annotation.SuppressLint;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.studyktdemo.R;
import com.example.studyktdemo.bean.HomeBean;
import com.example.studyktdemo.databinding.ItemHomeContentBinding;
import com.example.studyktdemo.databinding.ItemHomeHeaderBinding;
import com.example.studyktdemo.uitls.ResourceUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<HomeBean.Issue.Item> mHomeData;
    private HomeItemCLick mClickListener;

    private final int ITEM_TITLE_VIEW = 1;
    private final int ITEM_VIDEO_VIEW = 2;

    @NonNull
    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_VIDEO_VIEW) {
            return new VideoHolder(ItemHomeContentBinding.inflate(LayoutInflater.from(parent.getContext())));
        } else {
            return new TitleHolder(ItemHomeHeaderBinding.inflate(LayoutInflater.from(parent.getContext())));
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull @NotNull RecyclerView.ViewHolder holder, int position) {

        if (getItemViewType(position) == ITEM_VIDEO_VIEW) {
            VideoHolder videoHolder = (VideoHolder) holder;
            videoHolder.bindData(videoHolder, position, mHomeData);
        } else {
            TitleHolder titleHolder = (TitleHolder) holder;
            titleHolder.bindData(titleHolder, position, mHomeData);
        }

        //设置单个的点击事件
        holder.itemView.setOnClickListener(v -> {
            if (mClickListener != null) {
                mClickListener.onItemClickListener(position + 1);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mHomeData != null) {
            return mHomeData.size();
        }
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHomeData.get(position).getType().equals("textHeader")) {
            return ITEM_TITLE_VIEW;
        } else {
            return ITEM_VIDEO_VIEW;
        }
    }

    //Video的Holder
    public static class VideoHolder extends RecyclerView.ViewHolder {
        private ItemHomeContentBinding binding;

        public VideoHolder(@NonNull @NotNull ItemHomeContentBinding itemView) {
            super(itemView.getRoot());
            this.binding = itemView;
        }

        //绑定数据
        @SuppressLint("SetTextI18n")
        public void bindData(VideoHolder holder, int position, List<HomeBean.Issue.Item> mHomeData) {
            //标题
            holder.binding.tvTitle.setText(Objects.requireNonNull(mHomeData.get(position)
                    .getData()).getTitle());

            //详细描述
            holder.binding.tvTag.setText(Objects.requireNonNull(mHomeData.get(position)
                    .getData()).getDescription());

            //分类
            holder.binding.tvCategory.setText(ResourceUtils.getString(R.string.str_symbol) + Objects.requireNonNull(mHomeData.get(position)
                    .getData()).getTags().get(3).getName());

            //作者头像
            Glide.with(holder.itemView).load(Objects.requireNonNull(mHomeData.get(position)
                    .getData()).getAuthor().getIcon()).circleCrop().into(holder.binding.ivAvatar);

            //背景大图片
            Glide.with(holder.itemView).load(Objects.requireNonNull(mHomeData.get(position)
                    .getData()).getCover().getFeed()).into(holder.binding.ivCoverFeed);
        }
    }

    public static class TitleHolder extends RecyclerView.ViewHolder {
        private ItemHomeHeaderBinding homeHeaderBinding;

        public TitleHolder(@NonNull @NotNull ItemHomeHeaderBinding itemView) {
            super(itemView.getRoot());
            this.homeHeaderBinding = itemView;
        }

        public void bindData(TitleHolder titleHolder, int position, List<HomeBean.Issue.Item> mHomeData) {
            //homeHeaderBinding.tvHeader.setTypeface(typeface);
            homeHeaderBinding.tvHeader.setText(Objects.requireNonNull(mHomeData.get(position).getData()).getText());
        }
    }

    public void setData(@Nullable List<HomeBean.Issue.Item> itemList) {
        if (itemList != null) {
            this.mHomeData = itemList;
        }
        notifyDataSetChanged();
    }

    public void addHomeData(@NotNull List<HomeBean.Issue.Item> itemList) {
        for (HomeBean.Issue.Item item : itemList) {
            this.mHomeData.add(mHomeData.size(), item);
        }
        notifyDataSetChanged();
    }

    public interface HomeItemCLick {
        void onItemClickListener(int position);
    }

    public void setOnItemClick(HomeItemCLick click) {
        this.mClickListener = click;
    }
}
